home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / gfx / opal / lsidocs.lha / Source / Loaders / grad.c next >
Encoding:
C/C++ Source or Header  |  1993-06-14  |  5.5 KB  |  227 lines

  1.  
  2. /* This is just a quick and nasty example of using
  3.  * requesters in OpalPaint. This will bring up
  4.  * a few requesters and then load a gradient
  5.  * into OpalPaint.
  6.  * To use this loader, you must select it from the
  7.  * load format requester (Right mouse button on load gadget).
  8.  */
  9.  
  10. #include <proto/exec.h>
  11. #include <proto/dos.h>
  12. #include <opal/opallib.h>
  13. #include <opal/loadsave.h>
  14. #include <opal/opalpaint.h>
  15. #include <opal/macros.h>
  16. #include <proto/intuition.h>
  17. #include <proto/graphics.h>
  18. #include <graphics/gfxmacros.h>
  19. #include <intuition/intuition.h>
  20. #include <exec/types.h>
  21. #include <exec/ports.h>
  22. #include <exec/nodes.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26.  
  27.  
  28. /* NOTE: if your using callback functions in Build_Request()
  29.  * disable stack checking and enable SAVEDS (SAS/C).
  30.  */
  31.  
  32. #define WIDTH 320    /* Size of gradient that we'll generate */
  33. #define HEIGHT 256
  34.  
  35. struct OpalBase *OpalBase;
  36.  
  37.  
  38. struct MsgPort *LoadPort;
  39. BOOL Do_Grad (void);
  40.  
  41. char LoaderName[] = "Gradient";
  42. char PortName[] = "Gradient_Loader";
  43.  
  44. void main (void)
  45. {
  46.    struct LSIMessage *Mesg;
  47.  
  48.     LoadPort = CreatePort (PortName,0);
  49.     if (LoadPort==NULL)
  50.         exit (10);
  51.     OpalBase = (struct OpalBase *)OpenLibrary ("opal.library",0);
  52.     if (OpalBase==NULL)
  53.         { DeletePort (LoadPort);
  54.           exit (10);
  55.         }
  56.  
  57.     if (!AddOVLoader (LoaderName,LoadPort,OVLF_ALLOWIMAGE))
  58.         { DeletePort (LoadPort);
  59.           CloseLibrary ((struct Library *)OpalBase);
  60.           exit (10);
  61.         }
  62.     CloseLibrary ((struct Library *)OpalBase);    /* library must be closed */
  63.     OpalBase = NULL;
  64.  
  65.     while (1)
  66.         { WaitPort (LoadPort);
  67.           while (Mesg = (struct LSIMessage *)GetMsg (LoadPort))
  68.             { switch (Mesg->lsi_Type)
  69.                 { case OVCMD_LOADIMAGE:
  70.                     ReplyMsg ((struct Message *)Mesg);
  71.                     Do_Grad();
  72.                     break;
  73.                   case OVCMD_EXPUNGE:
  74.                     if (!OpalBase)
  75.                         OpalBase = (struct OpalBase *)OpenLibrary ("opal.library",0);
  76.                     RemOVLoader (LoaderName);
  77.                     if (OpalBase)
  78.                         CloseLibrary ((struct Library *)OpalBase);
  79.                     DeletePort (LoadPort);
  80.                     ReplyMsg ((struct Message *)Mesg);
  81.                     exit (0);
  82.                     break;
  83.                   case OVCMD_FORMATCHECK:
  84.                     Mesg->lsi_Result = TRUE;
  85.                     Mesg->lsi_Width = WIDTH;
  86.                     Mesg->lsi_Height = HEIGHT;
  87.                     Mesg->lsi_Depth = 24;
  88.                     Mesg->lsi_Flags = LSIF_HASIMAGE;
  89.                     ReplyMsg ((struct Message *)Mesg);
  90.                     break;
  91.                 }
  92.             }
  93.         }
  94. }
  95.  
  96.  
  97. UBYTE Red[WIDTH],Green[WIDTH],Blue[WIDTH];
  98.  
  99.  
  100. struct TextAttr Font = { (STRPTR)"topaz.font",8,0,0 };
  101. MakeBorder (GadBd,GadVec,0,0,200,25);
  102. MakeIBorder2 (GadIBd,GadVec,0,0,200,25);
  103.  
  104. MakeDoubleText (JPegLoadText,90,12,"Loading JPEG Image",NULL);
  105. MakeWText (JPSmoothText,46,10,"Smoothing On ");
  106. MakeButton (JPSmoothGad,NULL,66,60,200,25,JPSmoothText,GadBd,GadIBd,104);
  107.  
  108. struct Requester *ActiveReq;    /* The requester        */
  109. struct RastPort *ReqRP;        /* The requester's raster port    */
  110. struct GfxBase *GfxBase;
  111. struct IntuitionBase *IntuitionBase;
  112. BOOL BlockSmooth=TRUE;
  113.  
  114.  
  115. /* This init function simply saves the pointers */
  116.  
  117. __saveds static void SmoothReq_Init (struct RastPort *RP,struct Requester *Req)
  118. {
  119.     ReqRP = RP;
  120.     ActiveReq = Req;
  121. }
  122.  
  123. /* Handler function toggles the gadget text */
  124.  
  125. __saveds static void SmoothReq_Handler (void)
  126. {
  127.     if (BlockSmooth)
  128.         { BlockSmooth = FALSE;
  129.           JPSmoothText.IText[11]='f';
  130.           JPSmoothText.IText[12]='f';
  131.         }
  132.     else
  133.         { BlockSmooth = TRUE;
  134.           JPSmoothText.IText[11]='n';
  135.           JPSmoothText.IText[12]=' ';
  136.         }
  137.     SetAPen (ReqRP,OPGREY);
  138.     SetOPen (ReqRP,OPGREY);
  139.     RectFill (ReqRP,70,63,260,82);
  140.     RefreshGList (&JPSmoothGad,ActiveReq->RWindow,ActiveReq,1L);
  141. }
  142.  
  143.  
  144.     /* Tags for Build_Req_Tags() */
  145.  
  146. struct TagItem BRTags[] =
  147.     { {OPBR_Width,330},
  148.       {OPBR_Height,170},
  149.       {OPBR_Flags,BRF_CANCEL},
  150.       {OPBR_IText,(ULONG)JPegLoadText},
  151.       {OPBR_Gadgets,(ULONG)&JPSmoothGad},
  152.       {OPBR_InitFunc,(ULONG)SmoothReq_Init},
  153.       {OPBR_GadFunc,(ULONG)SmoothReq_Handler},
  154.       {OPBR_Flags,BRF_CANCEL|BRF_DRAGABLE},
  155.       {TAG_END,0}
  156.     };
  157.  
  158. BOOL Do_Grad (void)
  159. {
  160.    int i,j;
  161.    struct MsgPort *MasterPort;
  162.    struct LSIMessage Mesg;
  163.    BOOL Res;
  164.  
  165.     MasterPort = FindPort (OVLOADERPORT);
  166.     if (MasterPort==NULL) return (FALSE);
  167.  
  168.     IntuitionBase = (struct IntuitionBase *)OpenLibrary ("intuition.library",0L);
  169.     GfxBase = (struct GfxBase *)OpenLibrary ("graphics.library",0L);
  170.  
  171.     memset (&Mesg,0,sizeof(struct LSIMessage));
  172.     Mesg.lsi_Node.mn_Node.ln_Type = NT_MESSAGE;
  173.     Mesg.lsi_Node.mn_ReplyPort = LoadPort;
  174.     Mesg.lsi_Node.mn_Length = sizeof (struct LSIMessage);
  175.  
  176.     Mesg.lsi_Type = OVCMD_BUILDREQUEST;
  177.     Mesg.lsi_Address = (APTR)BRTags;
  178.     LSICmd (MasterPort,LoadPort,&Mesg);
  179.  
  180.     Mesg.lsi_Type = OVCMD_ASKUSER;
  181.     Mesg.lsi_Address = (APTR)"This is OVCMD_ASKUSER\\nHit ok or Cancel";
  182.     LSICmd (MasterPort,LoadPort,&Mesg);
  183.  
  184.     Res = Mesg.lsi_Result;
  185.  
  186.     Mesg.lsi_Type = OVCMD_USERMESSAGE;
  187.     if (Res)
  188.         Mesg.lsi_Address = (APTR)"OKAY!!";
  189.     else
  190.         Mesg.lsi_Address = (APTR)"CANCEL!!";
  191.     LSICmd (MasterPort,LoadPort,&Mesg);
  192.  
  193.  
  194.     for (j=0; j<HEIGHT; j++)
  195.         { for (i=0; i<WIDTH; i++)
  196.             { Red[i] = i;
  197.               Green[i] = j;
  198.               Blue[i] = (i+j)/2;
  199.             }
  200.           Mesg.lsi_Planes[0] = Red;
  201.           Mesg.lsi_Planes[1] = Green;
  202.           Mesg.lsi_Planes[2] = Blue;
  203.           Mesg.lsi_Width = WIDTH;
  204.           Mesg.lsi_Height = 1;
  205.           Mesg.lsi_X = 0;
  206.           Mesg.lsi_Y = j;
  207.           Mesg.lsi_Type = OVCMD_SENDDATA;
  208.           Mesg.lsi_SubType = OVDF_RGB;
  209.  
  210.           LSICmd (MasterPort,LoadPort,&Mesg);
  211.           if ((j & 0xF)==0)
  212.             { Mesg.lsi_Type = OVCMD_PERCENTAGE;
  213.               Mesg.lsi_Address = "Grad ";
  214.               Mesg.lsi_Result = (j*100)/HEIGHT;
  215.               LSICmd (MasterPort,LoadPort,&Mesg);
  216.             }
  217.         }
  218.  
  219.     Mesg.lsi_Type = OVCMD_DONE;
  220.     LSICmd (MasterPort,LoadPort,&Mesg);
  221.  
  222.     CloseLibrary ((struct Library *)IntuitionBase);
  223.     CloseLibrary ((struct Library *)GfxBase);
  224.     return (TRUE);
  225. }
  226.  
  227.